<?php
namespace app\admin\service;

use library\tools\Node;
use think\Db;
use think\facade\Request;
use app\admin\service\CompanyService;

/**
 * 服务点/服务次数管理服务
 * Class PointService
 * @package app\admin\service
 */
class PointService
{
    /**
     * 添加服务点充值/消费记录
     * @param int 		$mid  			用户ID
	 * @param int 		$handle_mid  	办理公司mid
     * @param int 		$contract_id  	合同ID
     * @param int 		$type  			充值/消费类型   1购买套餐   2购买服务   3退款   4解约
     * @param int 		$point          服务点数
     * @param string 	$desc			明细
	 * @param string 	$code			公司编号
     * @return array
     */
    public static function pointDetAdd($mid,$handle_mid,$contract_id,$type,$point,$desc,$code)
    {
    	$arr = ['1'=>'C','2'=>'X','3'=>'T','4'=>'J'];
		$code = $arr[$type].$code.date('Ym',time()).strtoupper(substr(randomStr('point'),6,10));  	
		$csr_id =  Db::name('system_contract')->where(['id'=>$contract_id])->value('csr_id');	
		$data = [
			'code' => $code,
    		'mid' => $mid,
    		'handle_mid' => $handle_mid,
    		'contract_id' => $contract_id,
    		'type' => $type,
    		'point' => $point,
    		'desc' => $desc,
    		'create_at' => time(),
    		'csr_id' => $csr_id,
    		'user_id' => intval(session('admin_user.id'))
    	]; 
    	$result = Db::name('system_point_detailed')->insertGetId($data);
		if($result){
			return $result; 
		}else{
			return 0;
		}               	
    }
	
	
	/**
     * 添加服务次数充值/消费记录
     * @param int 		$mid  			用户ID
	 * @param int 		$handle_mid  	办理公司mid
     * @param int 		$contract_id  	合同ID
	 * @param int 		$service_id  	服务ID
     * @param int 		$type  			充值/消费类型   1购买套餐   2购买服务   3退款   4解约
     * @param int 		$frequency      服务次数
     * @param string 	$desc			明细
	 * @param string 	$code			公司编号
     * @return array
     */
    public static function frequencyDetAdd($mid,$handle_mid,$contract_id,$service_id,$type,$frequency,$desc,$code,$specific = 0)
    {
    	$arr = ['1'=>'C','2'=>'X','3'=>'T','4'=>'J'];
		$code = $arr[$type].$code.date('Ym',time()).strtoupper(substr(randomStr('point'),6,10));  	
		$csr_id =  Db::name('system_contract')->where(['id'=>$contract_id])->value('csr_id');	
		$data = [
			'code' => $code,
    		'mid' => $mid,
    		'handle_mid' => $handle_mid,
    		'contract_id' => $contract_id,
    		'service_id' => $service_id,
    		'type' => $type,
    		'frequency' => $frequency,
    		'desc' => $desc,
    		'create_at' => time(),
    		'csr_id' => $csr_id,
    		'user_id' => intval(session('admin_user.id')),
    		'specific' => $specific,
    	];
    	$result = Db::name('system_frequency_detailed')->insertGetId($data);
		if($result){
			return $result; 
		}else{
			return 0;
		}               	
    }
	
	
	/**
     * 账户管理服务提交，检测次数或者点数
     * @param array $company_id
     * @return array
     */
    public static function deduction_check($company_id)
	{
		//获取账户管理服务id
		$service = Db::name('system_service')->where(['is_special'=>1])->find();
		//找到主公司
		$master = CompanyService::getMaster($company_id);
		$info = Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id'],'is_deleted'=>0])->find();
		if(!$info || $info['surplus'] <= 0){
			//没有次数，扣除点数			
			return 1;
		}else{
			return 2;
		}		
	}
	
	
	
	/**
     * 添加社保服务记录
     * @param int 		$mid  			用户ID
	 * @param int 		$type  			类型   1公司   2个人  
	 * @param int 		$service_id  	服务ID
	 * @param int 		$source  		来源   1后台   2网站
     * @return array
     */
    public static function serviceHandAdd($mid,$type,$service_id,$source)
    {	
		if($type == 1){
			$company = Db::name('system_company')->where(['mid'=>$mid])->find();
			$code = number_rule_max(14);			
			$person_id = 0;
			$name = '';
			$id_number = '';
			$handle_mid = $mid;
		}else{
			$person = Db::name('system_person')->where(['mid'=>$mid])->find();
			$code = number_rule_max(15);
			$company = Db::name('system_company')->where(['id'=>$person['company_id']])->find(); 
			$person_id = $person['id'];
			$name = $person['name'];
			$id_number = $person['id_number'];
			$handle_mid = $company['mid'];
		} 
		
		$contract = Db::name('system_contract')->where(['company_id'=>$company['id'],'status'=>1])->find();			
		if(!$contract){
			return $return = ['status'=>0,'msg'=>'合同状态异常，无法办理服务'];
		}
		if($contract['setmeal_type'] == 1){
			$service = Db::name('system_service')->where(['id'=>$service_id])->find();
			$point = $service['point'];
			 		
			//找到主公司
			$master = CompanyService::getMaster($company['id']);
			//判断扣点数还是扣次数
			$member_count = Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service_id,'is_deleted'=>0])->find();
			if(!$member_count || $member_count['surplus'] == 0){
				//没有次数，查找点数
				if($point > $master['point']){
					return $return = ['status'=>0,'msg'=>'服务点数不足，无法办理服务'];
				}
				$deduction = 1;
			}else{
				$deduction = 2;
				$point = 0;
			}
		}else{
			$deduction = 0;
			$point = 0;
		}
		
						 		
		$data = [
			'code' => $code,  		
    		'type' => $type,
    		'service_id' => $service_id,
    		'point'   => $point,
    		'mid' => $mid,	
    		'company_id' => $company['id'],
    		'person_id' => $person_id,
    		'name' => $name,
    		'id_number' => $id_number,
    		'contract_id' => $contract['id'],
    		'csr_id' => $contract['csr_id'],
    		'source' => $source,
    		'deduction'=>$deduction
    	]; 
    	$result = Db::name('system_service_handle')->insertGetId($data);
		if($result){
			if($type == 1){
				save_number_rule_max(14);
			}else{
				save_number_rule_max(15);
			}
			if($contract['setmeal_type'] == 1){
				$desc = $code.'-'.$service['title'];
				if($deduction == 1){
					$re = self::pointDetAdd($master['mid'],$handle_mid,$contract['id'],2,$point,$desc,$master['unique']);
					if($re){
						Db::name('system_company')->where(['id'=>$master['id']])->setDec('point',$point);
					}
				}else{
					$re = self::frequencyDetAdd($master['mid'],$handle_mid,$contract['id'],$service_id,2,1,$desc,$master['unique']);
					if($re){
						Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service_id])->setDec('surplus');
						Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service_id])->setInc('used');
					}
				}
			}									
			$return = ['status'=>1,'msg'=>'提交成功'];
		}else{
			$return = ['status'=>0,'msg'=>'提交失败，请稍后再试'];
		}  
		return $return;              	
    }
    
    
    /**
     * 根据服务类型和对象获取服务项目
	 * @param int 		$class  			服务类型
	 * @param int 		$target  			服务对象
     * @return array
     */
    public static function service_class($class,$target)
    {
    	$target_arr = [3];
		$target_arr[] = $target;
    	$list = Db::name('system_service')->where(['class'=>$class,'target'=>$target_arr,'is_deleted'=>0,'status'=>1,'is_special'=>0])->order('sort desc,id asc')->column('title','id');   
        return $list;
    }
    
    
    /**
     * 社保服务退款
     * @param int 		$id  	社保服务委托表ID
     * @return Boolean
     */
    public static function serviceRefund($id)
	{
		$handle = Db::name('system_service_handle')->where(['id'=>$id])->find();
		if($handle['deduction'] == 0){
			return true;
		}
		$service = Db::name('system_service')->where(['id'=>$handle['service_id']])->find();
		$company = Db::name('system_company')->where(['id'=>$handle['company_id']])->find();
		$desc = $handle['code'].'-'.$service['title'];
		//找到主公司
		$master = CompanyService::getMaster($company['id']);
		if($handle['deduction'] == 1){
			//返还点数	
			$re = self::pointDetAdd($master['mid'],$company['mid'],$handle['contract_id'],3,$handle['point'],$desc,$master['unique']);
			if($re){
				Db::name('system_company')->where(['id'=>$master['id']])->setInc('point', $handle['point']);
				return true;
			}else{
				return false;
			}
		}else{
			//返还次数
			$re = self::frequencyDetAdd($master['mid'],$company['mid'],$handle['contract_id'],$handle['service_id'],3,1,$desc,$master['unique']);
			if($re){
				Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$handle['service_id']])->setInc('surplus');
				Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$handle['service_id']])->setInc('back');
				return true;
			}else{
				return false;
			}
		}
	}
    
    
    /**
     * 导出社保服务--公司
     * @param array $id
     * @return array
     */
    public static function export_handle_company($id)
    {
    	$filename = '公司社保服务列表';
    	$titles = array(
    		array('code','服务编号'),
            array('company_name','公司名称'),                       
            array('service_name','服务名称'),
            array('point','服务点数'),
            array('source','数据来源'), 
            array('status','状态'),           
            array('create_at','委托日期'),
            array('csr_id','所属客服')
        );
    	$list = Db::name('system_service_handle')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			$service = Db::name('system_service')->column('title','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['service_name'] = $service[$v['service_id']];
				$arr[$k]['point'] = $v['point'];
	    		$arr[$k]['source'] = $v['source'] == 1 ? '后台' : '网站';
				$arr[$k]['status'] = ['1'=>'待审核 ','2'=>'待确认','3'=>'已办理','4'=>'无法办理'][$v['status']];
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }


	/**
	 * 导出社保服务--员工
     * @param array $id
     * @return array
     */
    public static function export_handle_person($id)
    {
    	$filename = '员工社保服务列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),                       
            array('id_number','身份证号'),
            array('company_name','公司名称'),
            array('service_name','服务名称'),
            array('point','服务点数'),
            array('source','数据来源'), 
            array('status','状态'),           
            array('create_at','委托日期'),
            array('csr_id','所属客服')
        );
    	$list = Db::name('system_service_handle')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			$service = Db::name('system_service')->column('title','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['name'] = $v['name'];
				$arr[$k]['id_number'] = ' '.$v['id_number'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['service_name'] = $service[$v['service_id']];
				$arr[$k]['point'] = $v['point'];
	    		$arr[$k]['source'] = $v['source'] == 1 ? '后台' : '网站';
				$arr[$k]['status'] = ['1'=>'待审核 ','2'=>'待确认','3'=>'已办理','4'=>'无法办理'][$v['status']];
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
	
	/**
	 * 导出服务账单
     * @param array $id
     * @return array
     */
    public static function export_point_detail($id)
    {
    	$filename = '服务账单列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('company_name','公司名称'),
            array('point','点数'),                       
            array('type','类型'),
            array('create_at','创建时间'),
            array('desc','服务说明'),
            array('user_id','操作人员')
        );
    	$list = Db::name('system_point_detailed')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','mid');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['company_name'] = $companys[$v['mid']];
				if(in_array($v['type'], [1,3])){
					$arr[$k]['point'] = '+'.$v['point'];
				}else{
					$arr[$k]['point'] = '-'.$v['point'];
				}		
				$arr[$k]['type'] = ['1'=>'充值','2'=>'已使用','3'=>'已退回'][$v['type']];		    		
				$arr[$k]['create_at'] = date('Y-m-d',$v['create_at']);
				$arr[$k]['desc'] = $v['desc'];
				$arr[$k]['user_id'] = $v['user_id'] ? user_list()[$v['user_id']] : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }


	/**
	 * 导出服务次数
     * @param array $id
     * @return array
     */
    public static function export_frequency_detail($id)
    {
    	$filename = '服务次数列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('company_name','公司名称'),
            array('frequency','次数'),                       
            array('type','类型'),
            array('create_at','创建时间'),
            array('desc','服务说明'),
            array('user_id','操作人员')
        );
    	$list = Db::name('system_frequency_detailed')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','mid');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['company_name'] = $companys[$v['mid']];
				if(in_array($v['type'], [1,3])){
					$arr[$k]['frequency'] = '+'.$v['frequency'];
				}else{
					$arr[$k]['frequency'] = '-'.$v['frequency'];
				}		
				$arr[$k]['type'] = ['1'=>'充值','2'=>'已使用','3'=>'已退回'][$v['type']];		    		
				$arr[$k]['create_at'] = date('Y-m-d',$v['create_at']);
				$arr[$k]['desc'] = $v['desc'];
				$arr[$k]['user_id'] = $v['user_id'] ? user_list()[$v['user_id']] : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    
    /**
	 * 导出社保账单
     * @param array $id
     * @return array
     */
    public static function export_insbill($id)
    {
    	$filename = '社保账单列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('company_name','公司名称'),
            array('account_period','所属账期'),                       
            array('receivable','应收金额'),
            array('arrival','实收金额'),
            array('balance','余额'),
            array('annex_name','附件'),
            array('status','状态'),
            array('create_at','创建时间'),
            array('csr_id','所属客服')
        );
    	$list = Db::name('system_insbill')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['account_period'] = $v['account_period'];
				$arr[$k]['receivable'] = $v['receivable'];
				$arr[$k]['arrival'] = $v['arrival'];
				$arr[$k]['balance'] = $v['balance'];
				$arr[$k]['annex_name'] = $v['annex_name'];
				$arr[$k]['status'] = ['1'=>'待发送','2'=>'待付款','3'=>'待核对','4'=>'已付款'][$v['status']];					    		
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    /**
	 * 导出补差账单
     * @param array $id
     * @return array
     */
    public static function export_makeupbill($id)
    {
    	$filename = '补差账单列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('name','姓名'),
    		array('id_number','身份证号'),
            array('account_period','所属账期'),                       
            array('receivable','应收金额'),
            array('arrival','实收金额'),
            array('balance','余额'),
            array('annex_name','附件'),
            array('status','状态'),           
            array('csr_id','所属客服'),
            array('create_at','创建时间'),
        );
    	$list = Db::name('system_makeup_bill')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$names = Db::name('system_person')->column('name','id');
			$id_numbers = Db::name('system_person')->column('id_number','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['name'] = $names[$v['person_id']];
				$arr[$k]['id_number'] = ' '.$id_numbers[$v['person_id']];
				$arr[$k]['account_period'] = $v['account_period'];
				$arr[$k]['receivable'] = $v['receivable'];
				$arr[$k]['arrival'] = $v['arrival'];
				$arr[$k]['balance'] = $v['balance'];
				$arr[$k]['annex_name'] = $v['annex_name'];
				$arr[$k]['status'] = ['1'=>'待发送','2'=>'待付款','3'=>'待核对','4'=>'已付款'][$v['status']];					    						
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    /**
	 * 导出公司财务账单
     * @param array $id
     * @return array
     */
    public static function export_financial_bill($id)
    {
    	$filename = '公司财务账单列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('company_name','公司名称'),
            array('bill_type','账单类型'),                       
            array('receivable','应收金额'),
            array('arrival','实收金额'),
            array('balance','账单余额'),
            array('cost_type','费用类型'),           
            array('create_at','创建时间'),
            array('status','状态'),
        );
    	$list = Db::name('system_financial_bill')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['bill_type'] = ['1'=>'公司社保账单','2'=>'签约订单','3'=>'公司增值订单','4'=>'个人补缴账单','5'=>'个人增值订单'][$v['bill_type']];
				$arr[$k]['receivable'] = $v['receivable'];
				$arr[$k]['arrival'] = $v['arrival'];
				$arr[$k]['balance'] = $v['balance'];
				$arr[$k]['cost_type'] = ['1'=>'社保费','2'=>'服务费'][$v['cost_type']];    		
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['status'] = ['1'=>'待付款','2'=>'待核对','3'=>'已付款','4'=>'已取消'][$v['status']];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    /**
	 * 导出员工财务账单
     * @param array $id
     * @return array
     */
    public static function export_employee_bill($id)
    {
    	$filename = '员工财务账单列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('name','姓名'),
    		array('id_number','身份证号'),
            array('bill_type','账单类型'),                       
            array('receivable','应收金额'),
            array('arrival','实收金额'),
            array('balance','账单余额'),
            array('cost_type','费用类型'),           
            array('create_at','创建时间'),
            array('status','状态'),
        );
    	$list = Db::name('system_financial_bill_person')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$names = Db::name('system_person')->column('name','id');
			$id_numbers = Db::name('system_person')->column('id_number','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['name'] = $names[$v['person_id']];
				$arr[$k]['id_number'] = ' '.$id_numbers[$v['person_id']];
				$arr[$k]['bill_type'] = ['1'=>'公司社保账单','2'=>'签约订单','3'=>'公司增值订单','4'=>'个人补缴账单','5'=>'个人增值订单'][$v['bill_type']];
				$arr[$k]['receivable'] = $v['receivable'];
				$arr[$k]['arrival'] = $v['arrival'];
				$arr[$k]['balance'] = $v['balance'];
				$arr[$k]['cost_type'] = ['1'=>'社保费','2'=>'服务费'][$v['cost_type']];    		
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['status'] = ['1'=>'待付款','2'=>'待核对','3'=>'已付款'][$v['status']];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    
    /**
	 * 导出公司交易明细
     * @param array $id
     * @return array
     */
    public static function export_transaction_details($id)
    {
    	$filename = '公司交易明细列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('company_name','公司名称'),
            array('trade_type','交易类型'),                       
            array('trade_mode','交易方式'),
            array('amount','金额'),
            array('cost_type','费用类型'),
            array('desc','描述'),           
            array('date','交易时间'),
        );
    	$list = Db::name('system_transaction_details')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['trade_type'] = ['1'=>'收款','2'=>'退款'][$v['trade_type']];
				$arr[$k]['trade_mode'] = ['1'=>'公对公转账','2'=>'支付宝转账','3'=>'微信支付','4'=>'现金支付','5'=>'其他方式'][$v['trade_mode']];
				$arr[$k]['amount'] = $v['amount'];
				$arr[$k]['cost_type'] = ['1'=>'社保费','2'=>'服务费'][$v['cost_type']];    		
				$arr[$k]['desc'] = $v['desc'];
				$arr[$k]['date'] = $v['date'];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    
    /**
	 * 导出员工交易明细
     * @param array $id
     * @return array
     */
    public static function export_deal_details($id)
    {
    	$filename = '员工交易明细列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('name','姓名'),
    		array('id_number','身份证号'),
            array('trade_type','交易类型'),                       
            array('trade_mode','交易方式'),
            array('amount','金额'),
            array('cost_type','费用类型'),
            array('desc','描述'),           
            array('date','交易时间'),
        );
    	$list = Db::name('system_transaction_details_person')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$names = Db::name('system_person')->column('name','id');
			$id_numbers = Db::name('system_person')->column('id_number','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['name'] = $names[$v['person_id']];
				$arr[$k]['id_number'] = ' '.$id_numbers[$v['person_id']];
				$arr[$k]['trade_type'] = ['1'=>'收款','2'=>'退款'][$v['trade_type']];
				$arr[$k]['trade_mode'] = ['1'=>'公对公转账','2'=>'支付宝转账','3'=>'微信支付','4'=>'现金支付','5'=>'其他方式'][$v['trade_mode']];
				$arr[$k]['amount'] = $v['amount'];
				$arr[$k]['cost_type'] = ['1'=>'社保费','2'=>'服务费'][$v['cost_type']];    		
				$arr[$k]['desc'] = $v['desc'];
				$arr[$k]['date'] = $v['date'];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    
    /**
	 * 导出公司增值账单
     * @param array $id
     * @return array
     */
    public static function export_cadditional_insbill($id)
    {
    	$filename = '公司增值账单列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('company_name','公司名称'),
            array('cost_type','费用类型'),                       
            array('receivable','应收金额'),
            array('arrival','实收金额'),
            array('balance','余额'),
            array('desc','描述信息'),
            array('csr_id','所属客服'),
            array('status','状态'),
            array('create_at','创建时间')           
        );
    	$list = Db::name('system_cadditional_bill')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['cost_type'] = ['1'=>'社保费','2'=>'服务费'][$v['cost_type']];
				$arr[$k]['receivable'] = $v['receivable'];
				$arr[$k]['arrival'] = $v['arrival'];
				$arr[$k]['balance'] = $v['balance'];
				$arr[$k]['desc'] = $v['desc'];
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
				$arr[$k]['status'] = ['1'=>'待发送','2'=>'待付款','3'=>'待核对','4'=>'已付款'][$v['status']];					    		
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));				
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    
    /**
	 * 导出员工增值账单
     * @param array $id
     * @return array
     */
    public static function export_appreciation_insbill($id)
    {
    	$filename = '员工增值账单列表';
    	$titles = array(
    		array('code','账单编号'),
    		array('name','姓名'),
    		array('id_number','身份证号'),
            array('cost_type','费用类型'),                       
            array('receivable','应收金额'),
            array('arrival','实收金额'),
            array('balance','余额'),
            array('desc','描述信息'),
            array('csr_id','所属客服'),
            array('status','状态'),
            array('create_at','创建时间')           
        );
    	$list = Db::name('system_appreciation_bill')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$names = Db::name('system_person')->column('name','id');
			$id_numbers = Db::name('system_person')->column('id_number','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['name'] = $names[$v['person_id']];
				$arr[$k]['id_number'] = ' '.$id_numbers[$v['person_id']];
				$arr[$k]['cost_type'] = ['1'=>'社保费','2'=>'服务费'][$v['cost_type']];
				$arr[$k]['receivable'] = $v['receivable'];
				$arr[$k]['arrival'] = $v['arrival'];
				$arr[$k]['balance'] = $v['balance'];
				$arr[$k]['desc'] = $v['desc'];
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
				$arr[$k]['status'] = ['1'=>'待发送','2'=>'待付款','3'=>'待核对','4'=>'已付款'][$v['status']];					    		
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));				
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
}
